package com.thyrsus.project.service.event;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.thyrsus.project.dao.event.EventDAO;
import com.thyrsus.project.dao.event.entity.Event;
import com.thyrsus.project.dao.user.UserDAO;
import com.thyrsus.project.object.EventObject;
import com.thyrsus.project.object.UserContext;

import com.google.appengine.api.datastore.Key;

@Service
public class EventService {
	
	@Autowired
	private EventDAO eventDao;
	
	@Autowired 
	private UserDAO userDao;

	
	public void log() {
		List<Event> events = eventDao.listEvents();
		for (int i = 0 ; i < events.size() ; i ++) {
			System.out.println("Event N"+(i+1)+" : "+events.get(i).getName());
		}
		
	}
	
	public void delete(Long id,UserContext user) {
		Event event = eventDao.getEvent(id);
		if (userDao.removeCreatedEvent(user,event)) {
			eventDao.remove(id);
		}
	}
	
	public void delete() {
		System.out.println("Will delete : ");
		log();
		eventDao.removeAll();
	}
	
	public void create(UserContext user, EventObject event) {
		Event newEvent = new Event(event);
		eventDao.add(newEvent);
		userDao.addCreatedEvent(newEvent, user);
	}
	
	public List<EventObject> getCreatedEvent(UserContext user) {
		List<EventObject> resultat = new ArrayList<EventObject>();
		for (Key event:userDao.getCreatedEvent(user)) {
			resultat.add(new EventObject(eventDao.getEvent(event.getId()))) ;
		}
		return resultat;
	}
	
}
